package com.wash.shoes.controller.pc;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.wash.shoes.domain.dto.ShopPageDto;
import com.wash.shoes.entity.ShopPo;
import com.wash.shoes.request.BindUserShopRequest;
import com.wash.shoes.request.GetUnbindDataRequest;
import com.wash.shoes.service.ShopService;
import com.wash.shoes.service.UserShopService;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author zhengyunpeng
 * @since 2024-08-27
 */
@Api(tags = {"PC-店铺"})
@RestController
@RequestMapping({"/dev-api/shop/", "/shop"})
public class ShopController {

    @Autowired
    ShopService shopService;
    @Autowired
    private UserShopService userShopService;

    @ApiOperation("获取门店列表")
    @PostMapping("/list")
    public R<Page<ShopPo>> pageList(@RequestBody ShopPageDto shopPageDto) {
        Page<ShopPo> page = shopService.pageList(shopPageDto);
        return R.ok(page);
    }

    @ApiOperation("修改新增门店信息,有id为修改没有为新增")
    @PostMapping("/saveUpdate")
    public R<ShopPo> add(@RequestBody ShopPo shopPo) {
        if (ObjectUtil.isEmpty(shopPo.getId())) {
            shopPo.setStatus(1);
            shopService.save(shopPo);
        } else {
            shopService.updateById(shopPo);
        }
        return R.ok(shopPo);
    }

    @ApiOperation("逻辑删除 修改 status为0")
    @PostMapping("/deleteByStatus")
    public R<ShopPo> deleteByStatus(String theId) {
        ShopPo shop = shopService.getById(theId);
        shop.setStatus(0);
        shopService.updateById(shop);
        // 修改状态为0 ，如果需要判断是否能删除，后期处理
        return R.ok(shop);
    }

    @ApiOperation("获取详情")
    @PostMapping("/getDetailById")
    public R<ShopPo> getDetailById(String theId) {
        ShopPo shop = shopService.getById(theId);
        return R.ok(shop);
    }

    @ApiOperation("批量绑定用户和门店关系")
    @PostMapping("/batchBindUserShop")
    public R<Boolean> batchBindUserShop(@RequestBody @Valid BindUserShopRequest bindUserShopRequest) {
        return R.ok(userShopService.batchBindUserShop(bindUserShopRequest));
    }

    @ApiOperation("获取未绑定/已绑定的列表")
    @PostMapping("/getBindList")
    public R<?> getUnBindList(@RequestBody @Valid GetUnbindDataRequest getUnbindDataRequest) {
        return R.ok(userShopService.getUnBindList(getUnbindDataRequest));
    }
}